package display
{
	import flash.display.Bitmap;
	import flash.display.Sprite;
	import flash.events.Event;
	import pool.IPoolObject;


	/**
	 * @author gcxk721
	 * 2012-6-18
	 * 下午1:38:45
	 */
	public class QhMovieClip extends Sprite implements IPoolObject
	{

		/**
		 * 显示信息
		 */
		private var frames : Vector.<QhBitmap>;

		/**
		 * 显示的位图
		 */
		public var bmp : Bitmap;

		/**
		 * 当前显示的帧
		 */
		protected var curIndex : int;

		/**
		 * 当前MovieClip拥有的最大帧数
		 */
		protected var maxIndex : int;

		/**
		 * 池对象当前状态
		 */
		protected var curState : Boolean = false;
		
		/**
		 * 是否有函数执行 
		 */
		protected var script : Boolean = false;
		
		/**
		 * 调用的函数 
		 */
		protected var scriptFunc : Function;
		
		/**
		 * 调用函数的帧数 
		 */
		protected var scriptIndex : int = -1;
		
		/**
		 * 当前正在显示的图品 
		 */
		public var curBitmap : QhBitmap;

		public function QhMovieClip()
		{
			super();
			ini();
		}

		/**
		 * 设置帧信息
		 * @param value
		 *
		 */
		public function setFrames(value : Vector.<QhBitmap>) : void
		{
			frames = value;
			for (var i:int = 0; i < frames.length; i++) 
			{
				frames[i].setParent(this);
			}
			curIndex = 0;
			maxIndex = frames.length - 1;
			gotoAndStop(1);
		}

		/**
		 * 清除当前显示的位图资源 
		 * 
		 */
		public function cleanShow() : void
		{
			bmp.bitmapData = null;
		}
		
		/**
		 * 初始化方法
		 *
		 */
		private function ini() : void
		{
			bmp = new Bitmap();
			addChild(bmp);
		}

		/**
		 * 跳到某帧开始播放
		 * @param index
		 *
		 */
		public function gotoAndPlay(index : uint) : void
		{
			curIndex = index;
			if (curIndex > maxIndex)
			{
				curIndex = maxIndex;
			}
			else if (curIndex < 0)
			{
				curIndex = 0;
			}
			skipFrame();
			play();
		}

		/**
		 * @param index
		 *
		 */
		public function gotoAndStop(index : uint) : void
		{
			curIndex = index;
			if (curIndex > maxIndex)
			{
				curIndex = maxIndex;
			}
			else if (curIndex < 0)
			{
				curIndex = 0;
			}
			skipFrame();
			stop();
		}

		/**
		 * 开始播放
		 *
		 */
		public function play() : void
		{
			if (!hasEventListener(Event.ENTER_FRAME))
			{
				addEventListener(Event.ENTER_FRAME, onEnterFrameHandler);
			}
		}

		/**
		 * 停止播放
		 *
		 */
		public function stop() : void
		{
			if (hasEventListener(Event.ENTER_FRAME))
			{
				removeEventListener(Event.ENTER_FRAME, onEnterFrameHandler);
			}
		}

		/**
		 * 帧事件
		 * @param evt
		 *
		 */
		protected function onEnterFrameHandler(evt : Event) : void
		{
			if (curIndex == maxIndex)
			{
				curIndex = 0;
			}
			else
			{
				++curIndex;
			}
			skipFrame();
			if (script && curIndex == scriptIndex)
			{
				scriptFunc();
			}
		}
		
		public function addFrameScript(index : int, func : Function) : void
		{
			scriptIndex = index - 1;
			if(func != null)
			{
				script = true;
			}
			else
			{
				script = false;
			}
			scriptFunc = func;
		}

		/**
		 * 跳帧
		 *
		 */
		protected function skipFrame() : void
		{
			if(frames != null)
			{
				var qhBmp : QhBitmap = frames[curIndex];
				curBitmap = qhBmp;
				if(qhBmp != null && qhBmp.bmd != null)
				{
					bmp.bitmapData = qhBmp.bmd;
					bmp.x = qhBmp.x;
					bmp.y = qhBmp.y;
				}
			}
		}

		/**
		 * 取得当前的帧
		 * @return
		 *
		 */
		public function get currentFrame() : uint
		{
			return curIndex + 1;
		}

		/**
		 * 获取总的帧数
		 */
		public function get totalFrames() : int
		{
			return maxIndex + 1;
		}

		public function destroy() : void
		{
			if (contains(bmp))
			{
				removeChild(bmp);
			}
			//位图信息在缓存器中  统一删除
//			for (var i : int = 0; i < frames.length; i++)
//			{
//				frames[i].destory();
//				frames[i] = null;
//			}
			frames = null;
			bmp = null;
		}

		public function isUsed() : Boolean
		{
			return curState;
		}

		public function released() : Boolean
		{
			curState = false;
			curIndex = 0;
			maxIndex = 0;
			scriptIndex = -1;
			scriptFunc = null;
			script = false;
			frames = null;
			curBitmap = null;
			return false;
		}

		public function used() : Boolean
		{
			curState = true;
			return false;
		}

	}
}
